package com.imooc.miaosha.controller;

import com.imooc.miaosha.domain.MiaoshaUser;
import com.imooc.miaosha.domain.User;
import com.imooc.miaosha.redis.OrderKey;
import com.imooc.miaosha.redis.RedisService;
import com.imooc.miaosha.redis.UserKey;
import com.imooc.miaosha.result.CodeMsg;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName:HelloController
 * @Author:Administrator
 * @Desspriction:写点注释吗
 * @Date:Createed in 9:58 2019-03-18
 */
@Controller
public class HelloController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;

    @RequestMapping("/helloDemo")
    @ResponseBody
    public String helloDemo()
    {
        return "hello Demo";
    }
    @RequestMapping("/hello")
    @ResponseBody
    public Result<String> hello(String s) {
       return Result.success(s);
        // return new Result(0, "success", "hello,imooc");
    }

    @RequestMapping("/helloError")
    @ResponseBody
    public Result<String> helloError() {
        return Result.error(CodeMsg.SERVER_ERROR);
        //return new Result(500102, "XXX");
    }

    @RequestMapping("/thymeleaf")
    public String  thymeleaf(Model model) {
        model.addAttribute("name", "Joshua");
        return "hello";
    }
    @RequestMapping("/getById/{id}")
    @ResponseBody
    public Result<User> getById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }
    @RequestMapping("/insert")
    @ResponseBody
    public Result<String> insert() {
        boolean b = userService.tx();
        if(b)
        { return Result.success("插入成功");}else
        {
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<User> redisGet() {
        User  user  = redisService.get(UserKey.getById, ""+1, User.class);
        return Result.success(user);
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<Boolean> redisSet() {
        User user  = new User();
        user.setId(1);
        user.setName("1111");
        redisService.set(UserKey.getById, ""+1, user);//存放在redis中的 key：UserKey:id1 值："{\"id\":1,\"name\":\"1111\"}"
        return Result.success(true);
    }
    @RequestMapping("/redis/setname")
    @ResponseBody
    public Result<Boolean> redisSetName() {

        redisService.set(UserKey.getByName, "张三","zhangsan");
        return Result.success(true);
    }
    @RequestMapping("/redis/setorder")
    @ResponseBody
    public Result<Boolean> redisOrder() {

        redisService.set(OrderKey.getByOrderId, "20160719","orderid");
        return Result.success(true);
    }
    @RequestMapping("/redis/setnx")
    @ResponseBody
    public Result<Boolean> redisSetNx() {
        User user  = new User();
        user.setId(2);
        user.setName("2222");
        redisService.set(UserKey.getByIdNx, ""+2, user);
        return Result.success(true);
    }
    @RequestMapping("/user/info")
    @ResponseBody
    public Result<MiaoshaUser> getUserInfo(MiaoshaUser user) {

        return Result.success(user);
    }
}
